--[[
从禁发短信集合KEYS[1]中读取[序列:号码],并读取其关联[KEYS[2]:序号:号码]HASH中的内容,返回指定长度的List

KEYS[1] : 短信序号队列
KEYS[2] : hash key前缀, 和KEYS[1]读取的字符串组成完整的hash key
ARGV[1] : 返回的最大list长度
v 1.0
--]]

local forbidzset = KEYS[1]
local hashprefix = KEYS[2]
local from = tonumber(ARGV[1])
local to = tonumber(ARGV[2])
local start = tonumber(ARGV[3])
local count = tonumber(ARGV[4])
local serid = ARGV[5]


local function main()
	local array = {}
	local index = 0

	local size = redis.call("zcard",forbidzset)

	if (size ~= nil and size ~= "" and size ~= false) then	
		--[[
		local list = redis.call("zrangebyscore", forbidzset, from, to, "limit", start, count)
		--]]
		local list = redis.call("zrange",forbidzset,start,count)
		for i,v in ipairs(list) do
			if (v ~= nil and v ~= "" and v ~= false) then
				local modelname = redis.call("hmget",hashprefix..":"..v,"RecvModuleName")	
				if(modelname[1] == serid) then
					local hashvalue = redis.call("hmget", hashprefix..":"..v,"Srv_node","OrgSerialNo","SendPort","User","Smcont","SmType","RecvModuleName" )
					index = index +1
					if (index >= count) then
						break
					end
					array[index] = hashvalue
					
					redis.call("zrem", forbidzset, v)
					redis.call("del", hashprefix..":"..v)
					
					end
			end
    
		end
	else 
		array = nil
	end
return array
end

return main()